//
//  EntityView.m
//  genePool9b2b
//
//  Created by Greg Dunn on 2/8/10.
//  Copyright 2010 Apple Inc. All rights reserved.
//

#import "SSEntityView.h"

#import "SSParticleExplosion.h"
#import "SSEntityController.h"
#import "SSGame.h"

@implementation SSEntityView

@synthesize emitter;
@synthesize controller;

- (void) updateAnimation:(CCAnimation *)animation{
	[self runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO] ]];
}

- (void) updateAnimationFast{
	[self updateAnimation:fastAnimation];
}

- (void) updateAnimationNormal{
	[self updateAnimation:normalAnimation];
}

- (CGPoint) updatePosition:(SSVector3D *)localForceVector{
	CMLog(@"PlayerView.updatePosition self.rotation:%f localForceVector:%f", self.rotation, localForceVector.rotation);
	self.position = ccp((self.position.x + localForceVector.x), (self.position.y + localForceVector.y));
	self.rotation = localForceVector.rotation;
	return self.position;
}

- (void)run{
	
}

#pragma mark Particle Methods

- (void)downAndDeathWithDuration:(ccTime)duration fallDuration:(ccTime)fallDuration endPosition:(CGPoint)endPosition{ 
	CMLog(@"in downAndDeathWithDuration: %d", self.position.y);
	[self stopAllActions];
	
	id fallAction = [CCAnimate actionWithDuration:fallDuration animation:[self animationByName:@"death"] restoreOriginalFrame:NO];
	id downAction = [CCMoveBy actionWithDuration: duration position: endPosition];
	id fallAndDownAction = [CCSpawn actions:fallAction, downAction, nil];
	
	id scaleAction = [CCSequence actions:[CCScaleTo actionWithDuration:.1f scale:.5f], [CCScaleTo actionWithDuration:.1f scale:2], nil];
	id explosionAndCleanAction = [CCCallFunc actionWithTarget:self selector:@selector(explosionAndClean)];
	
	CCSequence *sequence = [CCSequence actions:fallAndDownAction, scaleAction, explosionAndCleanAction, nil];
	
	[self runAction:sequence];
}

-(void)delete{
	CMLog(@"try to delete entityView");
	//[self removeFromParentAndCleanup:YES];
	[self.parent removeChild:self cleanup:YES];
}

-(void)explosionAndClean{
	CMLog(@"explosionAndClean");
	self.emitter = [SSParticleExplosion node];
	self.emitter.duration = 0.2f;
	
	[self.emitter setPosition:CGPointMake(self.position.x, self.position.y)]; //self.position.x, self.position.y
	[self.parent addChild: self.emitter z:10];
	
	[self.parent removeChild:self cleanup:YES];
	
}

-(void)setEmitterPosition
{
	emitter.position = ccp(self.position.x + 52, self.position.y + 28);
}

-(void)addTailParticleSun{
	self.emitter = [CCParticleSun node];
	[self addChild: emitter z:10];
	
	emitter.gravity = ccp(300, 0);
	emitter.startSize = 3;
	emitter.startSizeVar = 3;
	emitter.texture = [[CCTextureCache sharedTextureCache] addImage: @"fire.pvr"];
	
	[self setEmitterPosition];
	
}

-(void)draw{
	int health = [[controller.entityModel stats] hp];
	//health = 2;
	int maxHealth = [[controller.entityModel stats] maxHp];
	//maxHealth = 2;
	Direction pointPosition = [controller.entityModel pointPosition];
	
	static int lineBuffer = 10;
	int lineHeight = 3;
	int startY = (self.contentSize.height/2);
	int endY = (self.contentSize.height/2) + lineBuffer*health;
	//int endY = 10*health;

	int actualX = self.position.x + (self.contentSize.width/2) + lineHeight*2;
	
	switch (pointPosition) {
		case UP:
			actualX = self.position.x + (self.contentSize.width/2) + lineHeight*2;
			break;
		case DOWN:
			actualX = self.position.x + (self.contentSize.width/2) + lineHeight*2;
			break;
		case LEFT:
			actualX = 0;
			break;
		case RIGHT:
			actualX = self.contentSize.width/2 + 10;
			break;
		default:
			[super draw];
			return;
	};
	
	static int maxColor = 200;
	static int colorBuffer = 55;
	float percentage = ((float) health) / ((float) maxHealth);
	percentage = 1;
	int actualY = ((endY-startY) * percentage) + startY;
	actualY =  10*health + startY;
	int amtRed = ((1.0f-percentage)*maxColor)+colorBuffer;
	int amtGreen = (percentage*maxColor)+colorBuffer;
	glEnable(GL_LINE_SMOOTH);
	
	glLineWidth(lineHeight);        
	glColor4ub(amtRed,amtGreen,0,255);
	ccDrawLine(ccp(actualX, startY), ccp(actualX, actualY));

	[super draw];
}

-(void)run:(SSGame *)game{
	
}

@end
